package mamikeji.liaowa.ui.activity.circle;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import mamikeji.liaowa.R;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;
import mamikeji.liaowa.utils.MaxTextLength;

public class CircleReportActivity extends BaseActivity implements View.OnClickListener {
    public static final String TAG = "CircleReportActivity";
    private RelativeLayout relativeLayout_circle_report_query, rl_circle_report_text_content;
    private RadioGroup radioGroup_report_parent;
    private RadioButton radioButton_report_1, radioButton_report_2, radioButton_report_3, radioButton_report_4;
    private TextView textView_circle_report_title;

    private EditText editText_circle_report_text_content;

    private String report_count = "";

    private String fcid;
    private String fcname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_report);
        getBase();
        setTitleLeftBtn();
        setTitleName(R.string.circle_details_pop_report);

        Intent intent = getIntent();
        fcid = intent.getStringExtra("fcid");
        fcname = intent.getStringExtra("fcname");

        editText_circle_report_text_content = (EditText) findViewById(R.id.editText_circle_report_text_content);
        rl_circle_report_text_content = (RelativeLayout) findViewById(R.id.rl_circle_report_text_content);
        textView_circle_report_title = (TextView) findViewById(R.id.textView_circle_report_title);
        radioGroup_report_parent = (RadioGroup) findViewById(R.id.radioGroup_report_parent);
        radioButton_report_1 = (RadioButton) findViewById(R.id.radioButton_report_1);
        radioButton_report_2 = (RadioButton) findViewById(R.id.radioButton_report_2);
        radioButton_report_3 = (RadioButton) findViewById(R.id.radioButton_report_3);
        radioButton_report_4 = (RadioButton) findViewById(R.id.radioButton_report_4);

        relativeLayout_circle_report_query = (RelativeLayout) findViewById(R.id.relativeLayout_circle_report_query);

        rl_circle_report_text_content.setVisibility(View.GONE);

        relativeLayout_circle_report_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bool) {
                    report_count = editText_circle_report_text_content.getText().toString();
                } else if (report_count.equals("")) {
                    ForToast.showToast("请选择举报类别！");
                    return;
                }
                if (report_count.equals("")) {
                    ForToast.showToast("请输入举报理由！");
                    return;
                }
                getFeedback();
            }
        });

        radioButton_report_1.setOnClickListener(this);
        radioButton_report_2.setOnClickListener(this);
        radioButton_report_3.setOnClickListener(this);
        radioButton_report_4.setOnClickListener(this);

        String nameString = getResources().getString(R.string.circle_details_report_title);
        String nameFinal = String.format(nameString, fcname);
        textView_circle_report_title.setText(nameFinal);

        InputFilter[] filters = new InputFilter[]{new MaxTextLength(80)};
        editText_circle_report_text_content.setFilters(filters);
    }

    /**
     * 意见反馈
     */
    private void getFeedback() {
        showLoading();
        Map<String, String> formMap = new HashMap<>();

//        formMap.put(HttpKey.FEEDBACK_PHONE, ForContents.USER_PHONE);
//        formMap.put(HttpKey.FEEDBACK_USERCODE, ForContents.USER_CODE);
        //后台需要电话号码与usercode
        formMap.put(HttpKey.FEEDBACK_PHONE, ForPreference.getString(ForContents.USER_PHONE));
        formMap.put(HttpKey.FEEDBACK_USERCODE, ForPreference.getString(ForContents.USER_CODE));

        formMap.put(HttpKey.FEEDBACK_CONTENT, report_count);
        formMap.put(HttpKey.FEEDBACK_SOURCE, ForContents.GET_2);
        formMap.put(HttpKey.FEEDBACK_STYLE, "1");
        formMap.put(HttpKey.FEEDBACK_CRICLEID, fcid);
        //增加了机型，App版本，系统版本字段
        formMap.put(HttpKey.FEEDBACK_VERSION, ForPreference.getString(ForContents.APP_VERSION));
        formMap.put(HttpKey.FEEDBACK_PHONESTYLE, ForPreference.getString(ForContents.USER_PHONE_MODEL));
        formMap.put(HttpKey.FEEDBACK_PHONESYSTEM, ForPreference.getString(ForContents.USER_PHONE_SYSTEM_LEVEL));

        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.SAVE_FEEDBACK, formMap, new StringCallback() {

            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
                hideLoading();
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG, response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                        finish();
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                    hideLoading();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    boolean bool = false;

    @Override
    public void onClick(View view) {
        bool = false;
        rl_circle_report_text_content.setVisibility(View.GONE);
        radioButton_report_1.setChecked(false);
        radioButton_report_2.setChecked(false);
        radioButton_report_3.setChecked(false);
        radioButton_report_4.setChecked(false);
        switch (view.getId()) {
            case R.id.radioButton_report_1:
                radioButton_report_1.setChecked(true);
                report_count = "广告";
                break;
            case R.id.radioButton_report_2:
                radioButton_report_2.setChecked(true);
                report_count = "色情";
                break;
            case R.id.radioButton_report_3:
                radioButton_report_3.setChecked(true);
                report_count = "违反/政治敏感";
                break;
            case R.id.radioButton_report_4:
                bool = true;
                rl_circle_report_text_content.setVisibility(View.VISIBLE);
                radioButton_report_4.setChecked(true);
                report_count = "4";
                break;
        }
    }

}
